package com.example.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author anqi
 * @date 2021/8/6 14:20
 */
@RestController
public class ArthasController {

    @Override
    public String toString() {
        return super.toString();
    }

    /**
     * 内存泄漏
     */
    @GetMapping("/memoryLeak")
    public void memoryLeak() {
        while (true) {
            this.list();
        }
    }

    static class MemoryLeakObject {
        byte[] o = new byte[10 * 1024 * 1024];
    }
    static List<MemoryLeakObject> list = new ArrayList<>();

    private void list() {
        list.add(new MemoryLeakObject());
    }

    /**
     * 死循环
     */
    @GetMapping("/endlessLoop")
    public void endlessLoop() {
        Thread thread = new Thread(() -> {
            while (true);
        }, "endlessLoop");
        thread.start();
    }

    /**
     * 方法链路追踪
     */
    @GetMapping("/trace")
    public void trace() throws InterruptedException {
        this.trace1();
    }

    private void trace1() throws InterruptedException {
        System.out.println("======");
        TimeUnit.SECONDS.sleep(2);
        this.trace2();
    }

    private void trace2() throws InterruptedException {
        System.out.println("******");
        TimeUnit.SECONDS.sleep(3);
    }
}